package com.community.common.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.community.common.config.BizException;
import com.community.common.mapper.HouseMapper;
import com.community.common.pojo.PageRequest;
import com.community.common.pojo.domain.House;
import com.community.common.pojo.dto.HouseBatchDto;
import com.community.common.pojo.dto.HousePageQueryDto;
import com.community.common.util.CommonUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 房屋表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2020-08-02
 */
@Slf4j
@Service
public class HouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Resource
    BuildingService buildingService;

    public void batchAdd(HouseBatchDto batchDto) {
        if (ObjectUtils.isEmpty(batchDto) || StringUtils.isBlank(batchDto.getLdId()) || null == batchDto.getDys()
                || null == batchDto.getDyfw() || null == batchDto.getLchMin() || null == batchDto.getLchMax()) {
            throw new BizException("参数不能为空");
        }
        if (batchDto.getLchMin() > batchDto.getLchMax()) {
            throw new BizException("最小楼层数不能大于最大楼层数");
        }
        List<Integer> lchList = new ArrayList<>();
        for (int i = batchDto.getLchMin(); i <= batchDto.getLchMax(); i++) {
            lchList.add(i);
        }

        for (Integer lch : lchList) {
            for (int i = 1; i <= batchDto.getDys(); i++) {
                for (int j = 1; j <= batchDto.getDyfw(); j++) {
                    houseMapper.insert((House.builder()
                            .id(CommonUtil.getIdStr())
                            .ldId(batchDto.getLdId())
                            .dyh(i + "")
                            .fwztdm(2)
                            .lch(lch + "")
                            .mph(lch + getMph((i - 1) * (batchDto.getDyfw()) + j))
                            .build()));
                }
            }
        }

    }


    public List<House> listByLdId(Integer LdId) {
        LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(House::getLdId, LdId);
        return houseMapper.selectList(wrapper);
    }


    public List<House> pageQuery(PageRequest<HousePageQueryDto> pageQuery) {
        HousePageQueryDto dto = pageQuery.getCondition();
        LambdaQueryWrapper<House> wrapper = new LambdaQueryWrapper<>();
        if (!Objects.isNull(dto)) {
            if (StringUtils.isNotBlank(dto.getXqId())) {
                wrapper.in(House::getLdId, buildingService.getLdIdListByXqId(dto.getXqId()));
            }
            if (StringUtils.isNotBlank(dto.getLdId())) {
                wrapper.eq(House::getLdId, dto.getLdId());
            }
        }
        PageHelper.startPage(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        return houseMapper.selectList(wrapper);
    }

    /**
     * @Desc: 门牌号自动补0
     * @author zhu
     */
    private String getMph(int mph) {
        String m = mph + "";
        if (m.length() == 1) {
            return "0" + m;
        }
        return m;
    }

}
